package clientGUI;

import banvetauclient.BanVeTauClient;
import banvetauclient.Serializer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class Login extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtUser;
    private final JPasswordField txtPass;

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("Đăng nhập tài khoản");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 346, 300);
        final JFrame parentForm = this;
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("Tên tài khoản:");
        lblNewLabel.setBounds(26, 61, 79, 14);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Mật khẩu:");
        lblNewLabel_1.setBounds(26, 105, 62, 14);
        contentPane.add(lblNewLabel_1);

        txtUser = new JTextField();

        txtUser.setBounds(115, 50, 203, 36);
        contentPane.add(txtUser);
        txtUser.setColumns(10);

        JButton btnLogin = new JButton("\u0110\u0103ng nh\u1EADp");
        btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(AccountIsExist(txtUser.getText(), new String(txtPass.getPassword()))){
                    JOptionPane.showMessageDialog(null, "Login Successfull", "Notice", JOptionPane.INFORMATION_MESSAGE);
                    parentForm.setVisible(false);
                    BookTicket fr = new BookTicket();
                    //BookTicket.user = user;
                    fr.setVisible(true);
                    parentForm.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Login Failure", "Notice", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        btnLogin.setBounds(29, 150, 91, 23);
        contentPane.add(btnLogin);

        JButton btnSignUp = new JButton("\u0110\u0103ng k\u00FD");
        btnSignUp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                SignUp frame = new SignUp();
                frame.setVisible(true);
            }
        });
        btnSignUp.setBounds(127, 150, 91, 23);
        contentPane.add(btnSignUp);

        JButton btnExit = new JButton("Tho\u00E1t");
        btnExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                CloseFrame();
            }
        });
        btnExit.setBounds(227, 150, 91, 23);
        contentPane.add(btnExit);

        txtPass = new JPasswordField();
        txtPass.setBounds(115, 94, 203, 36);
        contentPane.add(txtPass);
    }

    private void CloseFrame() {
        super.dispose();
    }

    public static boolean AccountIsExist(String acc, String pass) {
        /*Session session = TicketUtil.getSessionFactory().openSession();
         List<Users> lst = null;
         try {
         String queryCommand = "FROM Users where username = :acc and is_active = 1";
         if (pass != null && !pass.isEmpty()) {
         queryCommand += " and password = :pass";
         }
         Query query = session.createQuery(queryCommand);

         query.setParameter("acc", acc.toLowerCase());
         if (pass != null && !pass.isEmpty()) {
         query.setParameter("pass", pass);
         }

         lst = query.list();
         } catch (HibernateException e) {

         e.printStackTrace();
         } finally {
         session.close();
         }

         if (lst != null) {
         if (lst.size() > 0) {
         user = lst.get(0);
         return true;
         }
         }*/
        try {
            String[] InfoAccount = new String[2];
            InfoAccount[0] = acc;
            InfoAccount[1] = pass;
            byte[] serialObject = Serializer.serialize(InfoAccount);
            BanVeTauClient.cos.write(1);
            BanVeTauClient.cos.write(serialObject.length);
            BanVeTauClient.cos.write(serialObject);
            BanVeTauClient.cos.flush();
            boolean result = BanVeTauClient.cis.readBoolean();
            System.out.println(result);
            return result;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }
}
